package com.lucas.rpc.framework.register.remote;

import com.lucas.rpc.framework.remote.URL;
import com.lucas.rpc.framework.serialize.KryoSerializer;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: LiuCheng
 * @description: 模拟远程注册中心（nacos,zookeeper）
 * @date: 2023-11-28 14:59
 **/
public class MapRemoteRegister {

    private static Map<String,List<URL>> map = new HashMap();

    private static int len;

    public static void register(String interfaceName, URL url) throws Exception {
        List<URL> urlList = map.getOrDefault(interfaceName,new ArrayList<>());
        urlList.add(url);
        map.put(interfaceName,urlList);

        //创建一个文件，用于consumer和provider共享这个map，否则两个进程无法共享map，就会导致，consumer无法发现服务
        File file = new File("/share.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
        oos.writeObject(map);
        oos.close();
    }

    public static List<URL> getURLs(String interfaceName) throws Exception{
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("/share.txt"));
        map = (Map<String,List<URL>>)ois.readObject();
        ois.close();
        return map.get(interfaceName);
    }
}
